package structure.stack;

import java.util.Stack;

public class MonotonousStack {
    public static void main(String[] args) {

    }

    public static int[][] process(int[] arr){
        int[][] res = new int[arr.length][2];
        for (int i = 0; i < arr.length; i++) {
            int left = -1;
            int right = -1;
            int cur = i-1;
            while (cur >= 0){
                if (arr[cur] < arr[i]){
                    left = cur;
                    break;
                }
                cur--;
            }
            cur = i+1;
            while (cur<arr.length){
                if (arr[i] > arr[cur]){
                    right = cur;
                }
                cur++;
            }
            res[i][0] = arr[left];
            res[i][1] = arr[right];
        }
        return res;
    }

    public int[][] getNearLessNoRepeat(int[] arr){
        int[][] ans = new int[arr.length][2];
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < arr.length; i++) {
            while (!stack.isEmpty() && arr[i] < arr[stack.peek()]){
                int popIndex = stack.pop();
                int leftLessIndex = stack.isEmpty() ? -1 : stack.peek();
                ans[popIndex][0] = leftLessIndex;
                ans[popIndex][1] = i;
            }
            stack.push(i);
        }
        while (!stack.isEmpty()){
            int popIndex = stack.pop();
            int leftLessIndex = stack.isEmpty() ? -1 : stack.peek();
            ans[popIndex][0] = leftLessIndex;
            ans[popIndex][1] = -1;
        }
        return ans;
    }

}
